package com.jackson.service.impl;

/*
 * Author: JacksonCoder
 * FileName: RabbitMqServiceImpl
 * Version: 1.0.0
 * Date: 2024/12/12
 */

import com.jackson.config.RabbitMQConfig;
import com.jackson.service.RabbitMqService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service("rabbitmqService")
public class RabbitMqServiceImpl implements RabbitMqService, RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }

    @Override
    public void sendMessage(String message) {
//        amqpTemplate.convertAndSend(RabbitMQConfig.DIRECT_EXCHANGE, RabbitMQConfig.DIRECT_ROUTING_KEY, message);
        CorrelationData correlationData = new CorrelationData("3453");
        rabbitTemplate.convertAndSend(RabbitMQConfig.DIRECT_EXCHANGE, RabbitMQConfig.DIRECT_ROUTING_KEY, message, correlationData);
        // 测试 returnCallback
//        rabbitTemplate.convertAndSend(RabbitMQConfig.DIRECT_EXCHANGE, "fasdoijvoad", message, correlationData);
    }

    @Override
    public void sendFanoutMessage(String message) {
        amqpTemplate.convertAndSend(RabbitMQConfig.FANOUT_EXCHANGE, "", message);
    }

    @Override
    public void sendRoutingMessage() {
        amqpTemplate.convertAndSend(RabbitMQConfig.DIRECT_EXCHANGE1, "warning2", "发送warning2级别的消息");
    }

    @Override
    public void sendTopicMessage() {
        //amqpTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE,"quick.orange.rabbit","飞快的橘色兔子");
        // amqpTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE,"lazy.orange.elephant","慢腾腾的橘色大象");
        // amqpTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE,"quick.orange.fox","quick.orange.fox");
        // amqpTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE,"lazy.brown.fox","lazy.brown.fox");
        amqpTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE, "quick.brown.fox", "quick.brown.fox");

    }

    /**
     * @param correlationData 消息唯一标识
     * @param ack             交换机是否成功收到消息 true成功  false失败
     * @param cause           失败原因
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("confirm方法被执行量..." + correlationData);
        if (ack) {
            System.out.println("交换机，消息接收成功" + cause);
        } else {
            System.out.println("交换机，消息接收失败" + cause);
            // 这里要有一些补发机制
            System.out.println("id " + correlationData.getId());
        }
    }

    /**
     * @param message    消息主体
     * @param replyCode  返回code
     * @param replyText  返回信息
     * @param exchange   交换机
     * @param routingKey 路由key
     */
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("return方法被执行...");
        System.out.println("消息主体：" + new String(message.getBody()));
        System.out.println("replyCode：" + replyCode);
        System.out.println("replyText：" + replyText);
        System.out.println("exchange：" + exchange);
        System.out.println("routingKey：" + routingKey);
    }
}
